import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Width Example'),
        ),
        body: HomePageWidget(),
      ),
    );
  }
}

class HomePageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageWidgetState();
  }

}
class HomePageWidgetState extends State<HomePageWidget> {

  @override
  void initState() {
    super.initState();
  }
  ///HitTestBehavior.deferToChild child接收事件就响应事件child不接受自身就也不接收
  ///HitTestBehavior.opaque 自身接受事件，自身下方的类似stack层级的不接受事件
  ///HitTestBehavior.translucent 自身接收事件，自身下方的类似stack层级的也接受事件
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
        builder: (context, constraints) {
          double width = constraints.maxWidth;
          double height = 200;
          return Listener(
            onPointerDown: (event) {
              print("down父");
            },
            onPointerUp: (event) {
              print("up父");
            },
            onPointerMove: (event) {
              print("move父");
            },
            onPointerCancel: (event) {
              print("cancel父");
            },
            behavior: HitTestBehavior.translucent,
            child: Stack(
              children: [
                Listener(
                  child: ConstrainedBox(
                    constraints: BoxConstraints.tight(Size(300.0, 300.0)),
                    child: DecoratedBox(decoration: BoxDecoration(color: Colors.red)),
                  ),
                  onPointerDown: (event) => print("first child"),
                ),
                Listener(
                  child: ConstrainedBox(
                    constraints: BoxConstraints.tight(Size(200.0, 200.0)),
                    child: Center(child: Text("左上角200*200范围内-空白区域点击")),
                  ),
                  onPointerDown: (event) => print("second child"),
                  //放开此行注释后，单词点击 first ,second都会响应，HitTestBehavior.opaque是不行的
                  behavior: HitTestBehavior.translucent,
                )


              ],
            ),
          );
        }
    );
  }
}

